package com.itheima.controller.backend;

import com.itheima.common.entity.PageResult;
import com.itheima.common.entity.QueryPageBean;
import com.itheima.common.entity.Result;
import com.itheima.common.utils.AliOssUtil;
import com.itheima.pojo.Setmeal;
import com.itheima.service.SetmealService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;
import java.util.UUID;

@RestController("backend")
@RequestMapping("/setmeal")
@Api(tags ="套餐相关接口")
@Slf4j
public class SetmealController {
    @Autowired
    private SetmealService setmealService;
    @Autowired
    private AliOssUtil aliOssUtil;
    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 新增套餐
     * @param checkgroupIds
     * @param setmeal
     * @return
     */
    @PostMapping("/add")
    @ApiOperation("新增套餐")
    public Result add(@RequestParam List<Integer> checkgroupIds, @RequestBody Setmeal setmeal){
        log.info("新增套餐：{}，关联的检查组id：{}",setmeal,checkgroupIds);
        setmealService.addSetmeal(checkgroupIds,setmeal);
        redisTemplate.opsForList().leftPush("setmealPic",setmeal.getImg());
        return new Result(true,"新增套餐成功");
    }

    /**
     * 编辑套餐
     * @param checkGroupIds
     * @param setmeal
     * @return
     */
    @PostMapping("/edit")
    @ApiOperation("编辑套餐")
    public Result edit(@RequestParam List<Integer> checkGroupIds, @RequestBody Setmeal setmeal){
        log.info("编辑套餐：{}，关联的检查组id：{}",setmeal,checkGroupIds);
        setmealService.editSetmealAndCheckGroup(checkGroupIds,setmeal);
        return new Result(true,"编辑套餐成功");
    }

    /**
     * 根据套餐id查询套餐
     * @param id
     * @return
     */
    @GetMapping("/findById")
    @ApiOperation("根据套餐id查询套餐")
    public Result findById(@RequestParam Integer id){
        log.info("根据套餐id查询套餐：{}",id);
        Setmeal setmeal = setmealService.findById(id);
        return new Result(true,"查询套餐成功",setmeal);
    }

    /**
     * 分页查询套餐
     * @param queryPageBean
     * @return
     */
    @PostMapping("findPage")
    @ApiOperation("分页查询套餐")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean){
        log.info("分页查询套餐：{}", queryPageBean);
        PageResult pageResult = setmealService.findPage(queryPageBean);
        return pageResult;
    }

    @PostMapping("/upload")
    @ApiOperation("上传图片")
    public Result upload(@RequestParam MultipartFile imgFile){
        log.info("上传图片：{}",imgFile);
        String originalFilename = imgFile.getOriginalFilename();
        String suffix = null;
        if (originalFilename != null) {
            suffix = originalFilename.substring(originalFilename.lastIndexOf("."));
        }
        String fileName = UUID.randomUUID() + suffix;
        try {
            String url = aliOssUtil.upload(imgFile.getBytes(), fileName);
            redisTemplate.opsForList().leftPush("setmealPicUpload",url);
            return new Result(true,"图片上传成功",url);
        } catch (IOException e) {
            e.printStackTrace();
            return new Result(false,"图片上传失败");
        }
    }

}
